import prisma from "@/src/db/db";
import { notFound } from "next/navigation";
import fs from "fs/promises";
import { NextResponse } from "next/server";
export const GET = async (req, { params }) => {
  const product = await prisma.product.findUnique({
    where: { id: params.id },
    select: {
      name: true,
      filePath: true,
    },
  });

  if (!product) {
    return notFound();
  }
  const { size } = await fs.stat(product.filePath);
  const file = await fs.readFile(product.filePath);
  const extension = product.filePath.split(".").pop();
  return new NextResponse(file, {
    headers: {
      "Content-Disposition": `attachment; filename="${product.name}.${extension}"`,
      "Content-Length": size.toString(),
    },
  });
};
